package com.teach.javafxclient.controller.base;

import com.teach.javafxclient.Globalvar;
import com.teach.javafxclient.MainApplication;
import com.teach.javafxclient.util.HttpUtil;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;
import kong.unirest.json.JSONArray;
import kong.unirest.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Liang Jianlin
 * @date 2024-05-06 02:37
 */
public class EditCourseInfoController {
    @FXML
    public Label tip;
    public ImageView courseTable;
    public VBox floatPane;
    private String courseId;
    @FXML
    public Button giveup;
    @FXML
    public Button submit;
    @FXML
    public ComboBox typeComboBox;
//    @FXML
//    public ComboBox termComboBox;
    @FXML
    public TextField credit;
    @FXML
    public ComboBox assessComboBox;
    @FXML
    public ComboBox collegeComboBox;
    @FXML
    public TextField courseLocation;
    @FXML
    public TextField to;
    @FXML
    public TextField from;
    @FXML
    public TextArea introduce;
    JSONObject data = new JSONObject();
    JSONArray colleges = new JSONArray();
    Map<String,Integer> collegeMap = new HashMap<>();
    Map<String,String> assessmentMap = new HashMap<>();
    Map<String,String> typeMap = new HashMap<>();
    CourseTableController CTC;
    public void initialize(){
        floatPane.setVisible(false);
        floatPane.setVisible(false);
        courseTable.setOnMouseClicked(e->{
            floatPane.setManaged(!floatPane.isManaged());
            floatPane.setVisible(!floatPane.isVisible());
        });
        assessmentMap.put("考核","GRADE");
        assessmentMap.put("考试","SCORE");
        typeMap.put("所有周","all");
        typeMap.put("双周","even");
        typeMap.put("单周","odd");
        ObservableList<String> collegeItem = FXCollections.observableArrayList();
        colleges = HttpUtil.get(Globalvar.baseUrl+"/api/colleges")
                .header("Content-Type", "application/json")
                .asJson().getBody().getArray();
        for(int i = 0;i < colleges.length();++i){
            collegeItem.add((String)((JSONObject)colleges.get(i)).get("collegeName"));
            collegeMap.put(String.valueOf(((JSONObject)colleges.get(i)).get("collegeName")), (Integer) ((JSONObject)colleges.get(i)).get("collegeId"));
        }
        collegeComboBox.setItems(collegeItem);
//        ObservableList<String> termItem = FXCollections.observableArrayList("2024春","2024暑","2024秋","2024冬");//FIXME:少值
//        termComboBox.setItems(termItem);
        ObservableList<String> typeItem = FXCollections.observableArrayList("单周","双周","所有周");
        typeComboBox.setItems(typeItem);
        ObservableList<String> assessItem = FXCollections.observableArrayList("考核","考试");
        assessComboBox.setItems(assessItem);
        submit.setOnAction(e->{
            data.put("courseId", courseId);
            data.put("collegeId", collegeMap.get(collegeComboBox.getValue()));
            ((JSONObject)data.get("time")).put("type",typeMap.get(typeComboBox.getValue()));
            data.put("credit", credit.getText());
            data.put("location",courseLocation.getText());
            data.put("description", introduce.getText());
            data.put("assessment", assessmentMap.get(assessComboBox.getValue()));
            data.getJSONObject("time").put("time",CTC.jsonArray);
            data.remove("termName");
            data.remove("collegeName");
            data.remove("teacherName");
            data.remove("courseId");
            HttpResponse<JsonNode> response= HttpUtil.put(Globalvar.baseUrl+"/api/courses/"+courseId)
                    .header("Content-Type", "application/json")
                    .body(data)
                    .asJson();
            System.out.println(data);
            int statusCode = response.getStatus();
            if(statusCode == 200){
                tip.setText("修改成功");
            }else{
                tip.setText("修改失败");
            }
            System.out.println("Status code: " + statusCode);
        });
    }
    public void set(String collegeS, String creditS, String locationS, String termS, String assessS, String fromS, String toS, String introduceS, String courseId, String typeS, JSONObject res) throws IOException {
        this.courseId = courseId;
        collegeComboBox.setValue(collegeS);
        credit.setText(creditS);
        courseLocation.setText(locationS);
        introduce.setText(introduceS);
        from.setText(fromS);
        to.setText(toS);
//        termComboBox.setValue(termS);
        assessComboBox.setValue(assessS);
        typeComboBox.setValue(typeS);
        data = res;
        FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("base/course-table.fxml"));
        Parent root = fxmlLoader.load();
        CTC = fxmlLoader.getController();
        CTC.set(res.getJSONObject("time").getJSONArray("time"));
        floatPane.getChildren().add(root);
    }
}
